<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserPlayer
 *
 * @author zangguoping
 */
class UserPlayer extends CActiveRecord {

    public $query;
    public $startDate;
    public $endDate;

    public function rules() {
        return array(
            array('player_name,player_file_id,player_server_id', 'required'),
            array('player_last_update,player_file_server_id,player_file_id,player_file_server_id', 'numerical'),
            array('player_name', 'length', 'max' => 30),
            array('player_create_user_ip', 'length', 'max' => 16),
            array('startDate,endDate', 'date', 'message' => Yii::t('default', '日期格式不正确'), 'format' => 'yyyy-MM-dd'),
        );
    }

    public function tableName() {

        return '{{user_player}}';
    }

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function attributeLabels() {
        return array(
            'player_id' => Yii::t('default', '编号'),
            'player_user_name' => Yii::t('default', '用户名'),
            'player_file_id' => Yii::t('default', '播放器的文件'),
            'player_last_update' => Yii::t('default', '观看时间'),
            'player_create_time' => Yii::t('default', '观看时间'),
            'player_server_id' => Yii::t('default', '视频所在服务器'),
            'player_create_user_ip' => Yii::t('default', '来源IP'),
            'filterDate' => Yii::t('default', '点播时间过滤'),
            'query' => Yii::t('default', '用户或、视频名、IP'),
            'player_video_title' => Yii::t('default', '视频名称')
        );
    }

    public function getSearch() {
        $criteria = new CDbCriteria();
        if ($this->query) {

            if (stripos($this->query, '.') !== false) {
                $criteria->addSearchCondition('player_create_user_ip', $this->query, true);
            } else {
                $criteria->addSearchCondition('player_user_name', $this->query, false);
                $criteria->addSearchCondition('player_video_title', $this->query, true, 'OR');
            }
        }
        if ($this->startDate && $this->endDate) {
            $stime = strtotime($this->startDate);
            $etime = strtotime($this->endDate) + 86400;
            if ($etime > $stime) {
                $criteria->addBetweenCondition('player_create_time', $stime, $etime);
            }
        }
        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => 5,
                    ),
                    'sort' => array(
                        'defaultOrder' => 'id DESC', //设置默认排序是create_time倒序
                    ),
                ));
    }

}
